package com.study.mike.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.study.mike.mapper.BookMapper;
import com.study.mike.model.entity.Book;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kh
 */
@RequestMapping("/Itext")
@RestController
public class ItextPdfController {
    @Resource
    private BookMapper bookMapper;

    @GetMapping("/pdf")
    public void pdf() throws DocumentException, IOException {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setHeader("content-Type", "application/pdf");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=test.pdf");

        Document document = new Document();
        try {
            PdfWriter.getInstance(document, response.getOutputStream());
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
        document.open();
        // 解决无法正常显示中文的问题
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        Font FontContext = new Font(bfChinese, 12, Font.BOLD);
        document.setPageCount(2);
        // 标题
        document.addTitle("我的第一个文件下载导出");
        // 作者
        document.addAuthor("James");
        // 主题
        document.addSubject("Personal Grade of Jack.Edward");
        // 关键字
        document.addKeywords("Simulator");
        // 创建者
        document.addCreator("Kicinio");


//      Image image =Image.getInstance("/xp.png");

        List<String> titleList = new ArrayList<>();
        titleList.add("bookAuthor");
        titleList.add("bookNo");
        titleList.add("bookPrice");
        List<Book> bookList = bookMapper.selectList(null);
        int i = 0;
        for (Book book : bookList) {
            PdfPTable tableContent = new PdfPTable(titleList.size());
            if (i == 0) {
                PdfPTable tableTitle = new PdfPTable(titleList.size());

                PdfPCell cellOne = new PdfPCell();
                cellOne.setPhrase(new Paragraph(titleList.get(0)));
                cellOne.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellOne.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellOne);

                PdfPCell cellTwo = new PdfPCell();
                cellTwo.setPhrase(new Paragraph(titleList.get(1)));
                cellTwo.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellTwo);

                PdfPCell cellThree = new PdfPCell();
                cellThree.setPhrase(new Paragraph(titleList.get(2)));
                cellThree.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTitle.addCell(cellThree);

                document.add(tableTitle);
            }
            PdfPCell cell = new PdfPCell();

            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(book.getBookName(), new Font(bfChinese, Font.NORMAL)));
            tableContent.addCell(cell);
            document.add(tableContent);

            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(book.getBookNo(), new Font(bfChinese)));
            tableContent.addCell(cell);
            cell.setBorderWidth(20);
            document.add(tableContent);

            cell = new PdfPCell();
            cell.setPhrase(new Paragraph(String.valueOf(book.getBookPrice())));
            //cell.setImage(image);
            tableContent.addCell(cell);
            document.add(tableContent);
            i++;

        }
        document.close();
    }
}
